<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2019/2/27
 * Time: 10:44
 */

namespace app\api2\controller\member;


use app\api\controller\BasicUserApi;
use think\Db;

/**
 * 找物控制器
 * Class Purchase
 * @package app\api2\controller\member
 */
class Purchase extends BasicUserApi
{
    /**
     * 发布采购信息
     * @return \think\response\Json
     */
    function release(){
        if($this->request->isPost()){
            $post=$this->request->only([
                'name','type','number','model','shipping','image','desc','address','pay_type','grab_sheet_end_time'
            ],'post');
            $post['grab_sheet_end_time']=strtotime($post['grab_sheet_end_time']);
            $post['mid']=UID;
            $post['status']=0;
            $post['addtime']=time();
            Db::name('purchase')
                ->insert($post);
            return json(['msg'=>'发布成功，请等待审核'],200);
        }
    }

    /**
     * 采购列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function lists(){
        $page_size=$this->request->get('page_size',10);
        $page_now=$this->request->get('page_now',1);
        $list=Db::name('purchase')
            ->page($page_now,$page_size)
            ->where('status',1)
            ->field('id,name,addtime,grab_sheet_end_time,image,status')
            ->order('id desc')
            ->select();
        foreach ($list as $k=>&$v){
            $v['addtime']=date('Y/m/d',$v['addtime']);
            $v['grab_sheet_end_time']=date('Y/m/d H',$v['grab_sheet_end_time']);
            $v['image_arr']=explode('|',$v['image']);
        }
        return json(['msg'=>'success','data'=>$list],200);
    }

    /**
     * 我发布的采购列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function mine(){
        $page_size=$this->request->get('page_size',10);
        $page_now=$this->request->get('page_now',1);
        $list=Db::name('purchase')
            ->where('mid',UID)
            ->page($page_now,$page_size)
            ->field('id,name,addtime,grab_sheet_end_time,image,status')
            ->order('id desc')
            ->select();
        foreach ($list as $k=>&$v){
            $v['addtime']=date('Y/m/d',$v['addtime']);
            $v['grab_sheet_end_time']=date('Y/m/d H',$v['grab_sheet_end_time']);
            $v['image_arr']=explode('|',$v['image']);
        }
        return json(['msg'=>'success','data'=>$list],200);
    }

    /**
     * 删除采购信息
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    function del(){
        $purchase_id=$this->request->get('purchase_id');
        Db::name('purchase')
            ->where('id',$purchase_id)
            ->where('mid',UID)
            ->delete();
        return json(['msg'=>'删除成功'],200);
    }

    /**
     * 结束抢单
     * @return \think\response\Json
     */
    function end_grab_sheet(){
        $purchase_id=$this->request->get('purchase_id');
        Db::name('purchase')
            ->where('id',$purchase_id)
            ->where('mid',UID)
            ->setField('status',2);
        return json(['msg'=>'结束成功'],200);
    }

    /**
     * 采购详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function detail(){
        $purchase_id=$this->request->get('purchase_id');
        $purchase=Db::name('purchase')
            ->alias('p')
            ->join('store_member m','m.id=p.mid')
            ->where('p.id',$purchase_id)
            ->field('m.nickname,m.phone,p.*')
            ->find();

        $purchase['image_arr']=explode('|',$purchase['image']);

        return json(['msg'=>'success','data'=>$purchase],200);
    }

}